﻿/****************************************************************************************************************
*                                                                                                               *
* Copyright (C) 2011 5173.com                                                                                   *
* This project may be copied only under the terms of the Apache License 2.0.                                    *
* Please visit the project Home Page http://bqqapicsharp.codeplex.com/ for more detail.                         *
*                                                                                                               *
****************************************************************************************************************/

namespace BQQAPIClient.Core.Network
{
    using System;
    using System.Collections.Generic;
    using System.Net.Sockets;

    internal class SocketAsyncEventArgsPool
    {
        private Stack<SocketAsyncEventArgs> pool;
        private readonly object locker = new object();

        internal SocketAsyncEventArgsPool(int capacity)
        {
            this.pool = new Stack<SocketAsyncEventArgs>(capacity);
        }

        internal int Count
        {
            get { return pool.Count; }
        }

        internal SocketAsyncEventArgs Pop()
        {
            lock (locker)
            {
                if (pool.Count > 0)
                {
                    return pool.Pop();
                }
                else
                {
                    return null;
                }
            }
        }

        internal void Push(SocketAsyncEventArgs item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            lock (locker)
            {
                pool.Push(item);
            }
        }
    }
}
